package cn.tedu.csms.shop.service.impl;

import cn.tedu.csms.base.exception.ServiceException;
import cn.tedu.csms.base.response.StatusCode;
import cn.tedu.csms.shop.pojo.dto.ShopLoginDto;
import cn.tedu.csms.shop.pojo.dto.ShopSaveParam;
import cn.tedu.csms.shop.mapper.ShopMapper;
import cn.tedu.csms.shop.pojo.dto.ShopQuery;
import cn.tedu.csms.shop.pojo.entity.Shop;
import cn.tedu.csms.shop.pojo.vo.ShopVO;
import cn.tedu.csms.shop.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    ShopMapper shopMapper;

    @Override
    public List<ShopVO> selectShops(ShopQuery shopQuery) {
        log.debug("查询店铺列表,参数:{}", shopQuery);
        List<ShopVO> list = shopMapper.selectShops(shopQuery);
        log.debug("查询店铺列表,结果:{}", list);
        return list;
    }

    @Override
    public void updateShop(ShopSaveParam shopSaveParam) {
        log.debug("更新店铺业务，参数={}", shopSaveParam);
        // 这里调用mapper的方法来更新店铺信息
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopSaveParam, shop);
        shop.setUpdateTime(new Date());
        shopMapper.updateById(shop); // 这里需要有一个updateById方法
    }

    @Override
    public void addShop(ShopSaveParam shopSaveParam) {
        log.debug("新增店铺业务，参数={}", shopSaveParam);
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopSaveParam, shop);
        shop.setCreateTime(new Date());
        shop.setPassword("654321");
        // 这里调用mapper的方法来新增店铺信息
        shopMapper.insert(shop); // 这里需要有一个insert方法
    }

    @Override
    public void deleteShop(Long id) {
        // 这里调用mapper的方法来删除店铺信息
        shopMapper.deleteById(id); // 这里需要有一个deleteById方法
    }

    @Override
    public ShopVO shopLogin(ShopLoginDto shopLoginDto) {
        log.debug("店铺登录业务，参数={}", shopLoginDto);
        ShopVO shopVO = shopMapper.selectByName(shopLoginDto.getShopName()).get(0);
        if (shopVO == null) {
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        } else if (!shopVO.getPassword().equals(shopLoginDto.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        } else {
            return shopVO;
        }
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("修改店铺状态业务：{}",id,status);
        Shop shop=new Shop();
        shop.setId(id);
        shop.setStatus(status);
        shopMapper.updateById(shop);
    }
}

